import './index.less';
import { Form, Input, Button } from 'antd';
import { UserOutlined, LockOutlined } from '@ant-design/icons';
import { request, history, connect } from 'umi';
//链接命名空间
export default connect(({ navbarlist }) => ({ navbarlist }))(Login);
// 其中密码需要加密用这段进行加密
import { encryptlogin } from '../../util/cryto.js';
//请求
import { loginStep, navBarlist } from '../../api/index';
import { setLocal } from '../../util/localset';
function Login(props) {
  const { dispatch } = props;
  const getMenuData = async () => {
    const { data } = await navBarlist({ parentId: 1001 });
    dispatch({
      type: 'navbarlist/setNavbar',
      payload: data,
    });
    history.push('/layout');
  };
  /* function getMockData() {
        request('/appservice/common/v1/getSomeData').then(res => {
            console.log(res);
        })
    } */
  /*  getMockData() */
  //登录
  const onFinish = async (values: any) => {
    const userAccount = values.username;
    const userPwd = encryptlogin(values.password);
    const data1 = {
      userAccount,
      userPwd,
    };
    const res = await loginStep(data1);
    console.log(res);
    setLocal('userid', Number(res.data.userId));
    //存头像
    setLocal('userImg', res.data.headPortrait);
    if (res.code == 200) {
      getMenuData();
    }
  };
  return (
    <div className="bglogin">
      <div className="formlogin">
        <h3 className="logintilte">勤学后台管理</h3>
        <Form
          name="normal_login"
          className="login-form"
          initialValues={{ remember: true }}
          onFinish={onFinish}
        >
          <Form.Item
            name="username"
            rules={[{ required: true, message: 'Please input your Username!' }]}
          >
            <Input
              prefix={<UserOutlined className="site-form-item-icon" />}
              placeholder="Username"
            />
          </Form.Item>
          <Form.Item
            name="password"
            rules={[{ required: true, message: 'Please input your Password!' }]}
          >
            <Input
              prefix={<LockOutlined className="site-form-item-icon" />}
              type="password"
              placeholder="Password"
            />
          </Form.Item>

          <Form.Item className="btnmargin">
            <Button
              style={{
                width: '100%',
                backgroundColor: '#1a267b',
                height: '45px',
                color: '#fff',
              }}
              htmlType="submit"
            >
              登录
            </Button>
            <p className="pDesc"> 使用手机小程序提示的账号密码登录</p>
          </Form.Item>
        </Form>
      </div>
    </div>
  );
}
